﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GUI.ModelLayer;
using GUI.RLAServiceReference;


namespace GUI.Tabpages
{
    /// <summary>
    /// Interaction logic for PageFriendsList.xaml
    /// </summary>
    public partial class PageFriendsList : Page
    {
        private RealLifeServiceClient _client;
        private User _loggedInUser ;

        public PageFriendsList(User user)
        {
            InitializeComponent();
            _client = new RealLifeServiceClient();
            _loggedInUser = user;

            ListFriends(user);
            ListPendingFriends(user);
            
            //Testing(user);
        }

        public void ListFriends(User user)
        {
            lbxFriends.DataContext = _client.GetFriendships(user);
        }

        public void ListPendingFriends(User user)
        {
            List<Friend> friendList = _client.GetPendingFriendships(_loggedInUser);
            foreach (var pendingFriendship in friendList)
            {
                pendingFriendship.LoggedIUser = _loggedInUser;
            }
            lbxPendingFriends.DataContext = friendList;
        }

        public void Testing(User user)
        {
            List<User> myUsers = new List<User>();

            for (int i = 0; i < 10; i++)
            {
                myUsers.Add(user);
            }

            lbxFriends.DataContext = myUsers;
        }

        private void lbxFriendsList_Clicked(object sender, MouseButtonEventArgs e)
        {
            var item = ItemsControl.ContainerFromElement(lbxFriends, e.OriginalSource as DependencyObject) as ListBoxItem;
            if (item != null)
            {
                User usr = (User) item.Content;
                MessageBox.Show(string.Format("You clicked: {0} {1}", usr.FirstName, usr.LastName));
            }
        }

        private void btnAccept_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            Friend friend = btn.DataContext as Friend;

            int errorcode = _client.SetFriendshipStatus(friend.Requester, friend.Requested, Friend.Status.Accepted);

            if (errorcode == 1)
            {
                ListFriends(_loggedInUser);
                ListPendingFriends(_loggedInUser);
                MessageBox.Show("Done!");
            }
            else
            {
                MessageBox.Show("Something went wrong! \r\n errorcode: " + errorcode);
            }
        }

        private void Load_Friends(object sender, RoutedEventArgs e)
        {
            ListFriends(_loggedInUser);
            ListPendingFriends(_loggedInUser);
        }
        
    }
}
